package com.xiyuan.smartutils.paging;


import com.fasterxml.jackson.annotation.JsonIgnore;

import java.util.ArrayList;
import java.util.List;

/**
 * 分页结果对象
 *
 * @author xiyuan-lgz 2023-11-02 @version v1.0.0 新建与整理
 */
public final class PageResult<T> implements IPage<T> {
    
    private transient long pageNo = 1;
    private transient long pageSize = 10;
    private transient long pageCount;
    
    private long total;
    private List<T> items;
    
    public PageResult() {
        this(1, 10);
    }
    
    public PageResult(int pageNo, int pageSize) {
        this(0, pageNo, pageSize, null);
    }
    
    public PageResult(long pageNo, long pageSize) {
        this(0, pageNo, pageSize, null);
    }
    
    public PageResult(long pageNo, long pageSize, List<T> dataList) {
        this(dataList.isEmpty() ? 0 : 1, pageNo, pageSize, dataList);
    }
    
    public PageResult(int pageNo, int pageSize, List<T> dataList) {
        this(dataList.isEmpty() ? 0 : 1, pageNo, pageSize, dataList);
    }
    
    public PageResult(int total, int pageNo, int pageSize, List<T> dataList) {
        this(dataList.isEmpty() ? 0L : 1L, pageNo, pageSize, dataList);
    }
    
    public PageResult(long total, long pageNo, long pageSize, List<T> dataList) {
        this.pageNo = pageNo <= 1 ? 1 : pageNo;
        this.pageSize = pageSize = pageSize <= 1 ? 1 : pageSize;
        this.items = dataList != null ? dataList : new ArrayList<T>(0);
        this.total = total;
        this.pageCount = (total - 1) / pageSize + 1;
    }
    
    @Override
    public long getPageNo() {
        return pageNo;
    }
    
    @Override
    public long getPageSize() {
        return pageSize;
    }
    
    @Override
    public List<T> items() {
        return null != items ? items : (items = new ArrayList<T>(0));
    }
    
    @Override
    public long total() {
        return total;
    }
    
    @Override
    @JsonIgnore
    public boolean hasPrev() {
        return pageNo > 1;
    }
    
    @Override
    @JsonIgnore
    public boolean hasNext() {
        return pageNo < pageCount;
    }
    
    public PageResult<T> setPageNo(int pageNo) {
        return this.setPageNo((long) pageNo);
    }
    
    public PageResult<T> setPageNo(Integer pageNo) {
        return this.setPageNo(pageNo == null ? 1L : pageNo.longValue());
    }
    
    public PageResult<T> setPageNo(long pageNo) {
        this.pageNo = pageNo <= 1 ? 1 : pageNo;
        return this;
    }
    
    public PageResult<T> setPageNo(Long pageNo) {
        this.pageNo = pageNo <= 1 ? 1 : pageNo;
        return this;
    }
    
    public PageResult<T> setPageSize(Long pageSize) {
        this.pageSize = pageSize == null || pageSize <= 0 ? 10 : pageSize;
        this.pageCount = (this.total - 1) / this.pageSize + 1;
        return this;
    }
    
    public PageResult<T> setPageSize(long pageSize) {
        this.pageSize = pageSize <= 1 ? 1 : pageSize;
        this.pageCount = (this.total - 1) / this.pageSize + 1;
        return this;
    }
    
    public PageResult<T> setItems(List<T> list) {
        this.items = list != null ? list : new ArrayList<T>(0);
        return this;
    }
    
    public PageResult<T> setTotal(Long total) {
        this.total = total <= 0 ? 0 : total;
        this.pageCount = (this.total - 1) / this.pageSize + 1;
        return this;
    }
    
    public PageResult<T> setTotal(long total) {
        this.total = total <= 0 ? 0 : total;
        this.pageCount = (this.total - 1) / this.pageSize + 1;
        return this;
    }
    
    @Override
    public String toString() {
        StringBuilder strb = new StringBuilder("PageResult:{")
                .append("pageNo:").append(this.pageNo).append(",")
                .append("pageSize:").append(this.pageSize).append(",")
                .append("pageCount:").append(this.pageCount).append(",")
                .append("total:").append(this.total);
        return strb.append("}").toString();
    }
}
